package com.msb;

import com.msb.service.MyPersonService;
import com.msb.service.impl.MyPersonServiceImpl;
import org.apache.zookeeper.*;

import java.io.IOException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

/**
 * @author sunpeng
 * @Date 2021-07-21 16:06
 */
public class ProviderMain {
    public static void main(String[] args) {

        try {
            //RMI
            MyPersonService myPersonService = new MyPersonServiceImpl(); //创建接口实例
            LocateRegistry.createRegistry(8081); //创建注册表
            String url = "rmi://127.0.0.1:8081/myPersonService";
            Naming.bind(url, myPersonService); //注册服务
            System.out.println("服务器启动成功");

            //zookeeper
            ZooKeeper zooKeeper = new ZooKeeper("10.1.20.89:2181", 30000,null);
            String content = zooKeeper.create("/rpc/provider", url.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println(content);
        } catch (AlreadyBoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
